import { PureComponent } from "react"
import store from "../../../store"
import { changeAgeAction } from "../../../store/actionCreators"
export default function UseStoreHOC(OriginComponent) {
  class Cpn extends PureComponent {
    constructor() {
      super()
      this.state = {
        age: store.getState().age
      }
    }

    onAdd(n) {
      store.dispatch(changeAgeAction(n))
    }

    componentDidMount() {
      store.subscribe(() => {
        this.setState({
          // 强制触发视图更新
          age: store.getState().age
        })
      })
    }

    render() {
      return <OriginComponent onAdd={this.onAdd} {...this.state} />
    }
  }

  return Cpn
}
